package edu.uwlax.cs.oayonlinestore.client.gui.salesdept;

import java.util.Iterator;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.gui.Store;
import edu.uwlax.cs.oayonlinestore.client.gui.shared.Const.RetexStatus;
import edu.uwlax.cs.oayonlinestore.client.utils.FormTable;
import edu.uwlax.cs.oayonlinestore.client.utils.SelectionList;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.vo.OrderExtendedVO;
import edu.uwlax.cs.oayonlinestore.vo.OrderItemVO;
import edu.uwlax.cs.oayonlinestore.vo.RetExFormVO;

public class PageSalRetexEdit extends TitlePage {

	private class RetexEdit extends FormTable {

		private HTML date = new HTML();

		private TextArea description = new TextArea();

		private HTML name = new HTML();

		private SimplePanel oiCont = new SimplePanel();

		private SelectionList orderItem = new SelectionList();

		private RetExFormVO retex;

		private SelectionList status = new SelectionList(RetexStatus.ALL);
		
		private TextBox quantity  = new TextBox();

		public RetexEdit() {
			super();
			addInput("Date", date);
			addInput("Customer", name);
			addInput("Product", oiCont);
			addInput("Status", status);
			addInput("Quantity", quantity);
			addInput("Description", description);
			addButton("Save", Store.getFrame().getSalListRetex().getAnchor());
		}

		public void createRetEx(int orderID) {
			OnlinestoreRService.Util.getInstance().getOrderExtended(orderID,
					getLoadHandler());
		}

		private void loadOrderExtended(OrderExtendedVO o) {
			retex = new RetExFormVO(0, "", 0, null, "", null, null);
			name.setText(o.getUser().getFirstname() + " "
					+ o.getUser().getFirstname());
			oiCont.setWidget(orderItem);
			orderItem.clear();
			for (Iterator i = o.getOrderItems().iterator(); i.hasNext();) {
				OrderItemVO v = (OrderItemVO) i.next();
				orderItem.addItem(v.getProduct().getBrand() + " - "
						+ v.getProduct().getName(), String.valueOf(v.getId()));
			}
			status.setSelectedIndex(0);
			date.setText("");
			quantity.setText("1");
			description.setText("");
		}

		public void loadRetEx(int retexID) {
			OnlinestoreRService.Util.getInstance().getRetExForm(retexID,
					getLoadHandler());
		}

		private void loadRetExForm(RetExFormVO r) {
			retex = r;
			name.setText(r.getUser().getFirstname() + " "
					+ r.getUser().getLastname());
			oiCont.setWidget(new HTML(r.getOrderItem().getProduct().getBrand()
					+ " - " + r.getOrderItem().getProduct().getName()));
			status.setSelection(r.getStatus());
			date.setText(r.getDate() + "");
			quantity.setText(r.getQuantity()+"");
			description.setText(r.getDescription());
		}

		public void onButtonClicked() {
			int id = (retex.getId() == 0) ? Integer.valueOf(
					orderItem.getSelection()).intValue() : 0;
			retex.setStatus(status.getSelection());
			retex.setQuantity(Integer.parseInt(quantity.getText()));
			retex.setDescription(description.getText());
			OnlinestoreRService.Util.getInstance().addEditRetExForm(retex, id,
					getSaveHandler());
		}

		public void onDataLoad(Object data) {
			if (data instanceof OrderExtendedVO)
				loadOrderExtended((OrderExtendedVO) data);
			else if (data instanceof RetExFormVO)
				loadRetExForm((RetExFormVO) data);
		}

	}

	private RetexEdit edit;

	protected void create() {
		setTitle("Create/Edit Return/Exchange Form");
		add(edit = new RetexEdit());
	}

	public String getAnchor(boolean create, int id) {
		return getAnchor(((create) ? "create" : "edit") + "-" + id);
	}

	public String getID() {
		return "RetexForm";
	}

	protected boolean onShow(String param) {
		String[] s = param.split("-");
		int id = Integer.parseInt(s[1]);
		if (s[0].equals("create")) {
			setTitle("Create Return/Exchange Form");
			edit.createRetEx(id);
		} else {
			setTitle("Edit Return/Exchange Form");
			edit.loadRetEx(id);
		}
		return true;
	}

}
